#!/bin/bash

IFS_BAK=$IFS
IFS="
"

inv="/Data/inventory"
invT="/Data/inventory/temp"
invRW1="/Data/inventory/temp/raw1"
invRW2="/Data/inventory/temp/raw2"
mediaA="/Data/ShowA/"
mediaB="/Data/ShowB/"
mediaC="/Data/ShowC/"

echo "Generating TVShow List....."

/usr/bin/find $mediaA -size +5M -exec ls -sdh --block-size=M {} + |sed -e 's#M \/Data\/# /\Data\/#' > $invRW1 
/usr/bin/find $mediaB -size +5M -exec ls -sdh --block-size=M {} + |sed -e 's#M \/Data\/# /\Data\/#' >> $invRW1 
/usr/bin/find $mediaC -size +5M -exec ls -sdh --block-size=M {} + |sed -e 's#M \/Data\/# /\Data\/#' >> $invRW1

echo "Transforming List to CSV file"

/usr/bin/sed -e 's/^/"/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/M \//M","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/\//","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/ S0/","S0/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/ S1/","S1/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/ S2/","S2/g' $invRW2 > $invRW1
#/usr/bin/sed -e 's/ S3/","S3/g' $invRW1 > $invRW2
#/usr/bin/sed -e 's/ -  /","/g' $invRWi > $invRW2
/usr/bin/sed -e 's/0 - /0","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/1 - /1","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/2 - /2","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/3 - /3","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/4 - /4","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/5 - /5","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/6 - /6","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/7 - /7","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/8 - /8","/g' $invRW1 > $invRW2
/usr/bin/sed -e 's/9 - /9","/g' $invRW2 > $invRW1
/usr/bin/sed -e 's/$/"/g' $invRW1 > $invRW2
#/usr/bin/sed -e 's/\./","/g' $invRW1 > $invRW2

echo "Finalizing CSV File"

/bin/echo ""File Size \(MB\)","Root Directory","Sub Directory","Show Directory","Season Directory","Show Title","Episode ID","Episode Title"" > $inv/TVshow`date +%F`.csv
/usr/bin/cat $invRW2 >> $inv/TVshow`date +%F`.csv


echo "DONE Generating - `date`"
IFS=$IFS_BAK
IFS_BAK=
